package com.baosight.risk.web.rire;

import com.baosight.risk.api.rire.RireRiskEventService;
import com.baosight.risk.base.controller.BaseController;
import com.baosight.risk.base.entity.MultipleUpdateStatus;
import com.baosight.risk.common.annotation.Log;
import com.baosight.risk.common.bean.BeanUtils;
import com.baosight.risk.common.enums.BusinessType;
import com.baosight.risk.common.enums.IntegralType;
import com.baosight.risk.common.result.AjaxResult;
import com.baosight.risk.common.utils.DateUtils;
import com.baosight.risk.common.utils.ExcelUtil;
import com.baosight.risk.common.utils.StringUtils;
import com.baosight.risk.service.rire.entity.RireRiskEvent;
import com.baosight.risk.service.rire.vo.RireRiskEventVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.core.MediaType;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/rire/event")
@Api(value = "风险事件", description = "风险事件", produces = MediaType.APPLICATION_JSON)
public class RireRiskEventController extends BaseController {

    @Autowired
    private RireRiskEventService eventService;

    /**
     * 重点风险事件查询
     *
     * @param
     * @return
     */
    @GetMapping("getEventList")
    @ApiOperation("重点风险事件查询")
    @Log(integralType = IntegralType.Type2)
    public AjaxResult getEventList(RireRiskEventVO event) {
        return getResultPage(eventService.getEventList(event, getUser()));
    }

    /**
     * 重点风险事件查询
     *
     * @param
     * @return
     */
    @GetMapping("selectEventList")
    @ApiOperation("首页重点风险事件查询")
    @Log(integralType = IntegralType.Type2)
    public AjaxResult getList(RireRiskEventVO event) {
        return getResultPage(eventService.selectList(event, getUser()));
    }

    @PostMapping("/deleteEventByIds")
    @Log(title = "批量删除风险事件", businessType = BusinessType.DELETE)
    @ApiOperation("批量删除风险事件")
    public AjaxResult deleteEventList(@RequestBody MultipleUpdateStatus updateStatus) {
        if (CollectionUtils.isEmpty(updateStatus.getIds())) {
            log.error("请至少选择一条记录");
            return error("请至少选择一条记录");
        }
        Boolean flag = eventService.deleteByIds(updateStatus);
        if (!flag) {
            log.error("有数据不符合操作规范！");
            return error("有数据不符合操作规范！");
        }
        return success();
    }

    @PostMapping("/saveRireEvent")
    @Log(title = "新增/修改风险事件", businessType = BusinessType.INSERT)
    @ApiOperation("新增风险事件")
    public AjaxResult addEvent(@RequestBody List<RireRiskEvent> rireRiskEventList) throws Exception {
        if (CollectionUtils.isEmpty(rireRiskEventList)) {
            log.error("数据为空！");
            return error("数据为空！");
        }
        return success(eventService.saveRireEvent(rireRiskEventList, getUser()));
    }

    @PostMapping("/exportRireEvent")
    @ApiOperation("导出风险事件")
    public AjaxResult exportExcel(@RequestBody(required = false) RireRiskEventVO rireRisk) {
        log.info("导出风险事件信息：" + rireRisk.toString());
        ExcelUtil<RireRiskEvent> excel = new ExcelUtil<>(RireRiskEvent.class);
        List<RireRiskEvent> rireRiskEvents = eventService.selectList(rireRisk, null);
        return excel.exportExcel(rireRiskEvents, "风险事件", rireRisk.getColumNameList());
    }

}
